package com.yxhl.zoume.core.car.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;
import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.custom.RevealLayout;
import com.yxhl.zoume.core.func.map.ui.event.FinishLocationSelectEvent;
import com.yxhl.zoume.core.func.map.ui.event.SelectedPOIEvent;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.utils.LOG;
import com.yxhl.zoume.utils.PromptUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by sugarya on 16/4/13.
 */
public class CarLocationMapAdapter extends RecyclerView.Adapter<CarLocationMapAdapter.POIViewHolder> {

    private static final String TAG = "CarLocationMapAdapter";

    private List<PoiItem> mPoiItems;
    private Context mContext;
    private RecyclerView.LayoutManager mLayoutManager;

    public CarLocationMapAdapter(Context context,
                                 List<PoiItem> mPoiItems,
                                 RecyclerView.LayoutManager layoutManager) {
        this.mPoiItems = mPoiItems;
        this.mContext = context;
        this.mLayoutManager = layoutManager;
    }

    @Override
    public POIViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_car_location_map, parent, false);
        return new POIViewHolder(view);
    }

    @Override
    public void onBindViewHolder(POIViewHolder holder, int position) {
        PoiItem poiItem = mPoiItems.get(position);
        if (poiItem != null) {
            String title = poiItem.getTitle();
            String describe = poiItem.getProvinceName() + poiItem.getCityName() + poiItem.getAdName();

            holder.setPoiTitle(title, position);
            holder.setPoiDescribe(describe);
        }
    }

    @Override
    public int getItemCount() {
        return mPoiItems.size();
    }

    public void notifyItemData(List<PoiItem> poiItemList) {
        if (poiItemList != null) {
            int previousSize = mPoiItems.size();
            mPoiItems.clear();
            notifyItemRangeRemoved(0, previousSize);
            mPoiItems.addAll(poiItemList);
            notifyItemRangeInserted(0, poiItemList.size());
        }
    }

    protected class POIViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.linear_item_special_car_station_map)
        RevealLayout mLinearRoot;

        @BindView(R.id.tv_item_special_car_station_poi_title)
        TextView mPoiTitleTv;

        @BindView(R.id.tv_item_special_car_station_poi_describe)
        TextView mPoiDescribeTv;


        public POIViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @OnClick(R.id.linear_item_special_car_station_map)
        public void onPopupItemClick(View view) {
            int position = mLayoutManager.getPosition(view);
            if (position < mPoiItems.size() && position >= 0) {
                PoiItem poiItem = mPoiItems.get(position);
                if (poiItem != null) {
                    String title = poiItem.getTitle();
                    String adCode = poiItem.getAdCode();
                    LOG.e(TAG, "onPopupItem title=" + title);
                    LOG.e(TAG, "onPopupItem adCode=" + adCode);

                    if (!TextUtils.isEmpty(adCode) && !TextUtils.isEmpty(title)) {
                        RxBus.getInstance().send(new SelectedPOIEvent(poiItem));
                        RxBus.getInstance().send(new FinishLocationSelectEvent());
                    } else {
                        PromptUtils.showShort(mContext, mContext.getString(R.string.special_car_poi_empty));
                    }
                }
            }
        }

        void setPoiTitle(String title, int position) {
            if (!TextUtils.isEmpty(title)) {
                if (position == 0) {
                    mPoiTitleTv.setTypeface(Typeface.DEFAULT_BOLD);
                    mPoiTitleTv.setText("当前位置:  " + title);
                } else {
                    mPoiTitleTv.setTypeface(Typeface.DEFAULT);
                    mPoiTitleTv.setText(title);
                }

            }
        }

        void setPoiDescribe(String describe) {
            if (!TextUtils.isEmpty(describe)) {
                mPoiDescribeTv.setText(describe);
            }
        }

    }

}
